/* ******************************************************************
*                                                                   *
* This game operates as a client/server application. The IMME is    *
* the client and the donsdongle is the server. The IMME initiates   *
* communcations, manages data request, monitors exchange state,     *
* and desplays results back to the user via the LCD. The server     *
* simply processes requests by monitoring for a proper exchange,    *
* testing a security code for authroization, and returning an       *
* information request.                                              *
*                                                                   *
* Packet exchanging goes like this.                                 *
* IMME Sends Init Packet to initiate conversation    : initbyte --> *
* Don's Dongle Receives and sends OK or NOK          : <-- ok       *
* IMME Sends Security Code Packet with code          : secbyte -->  *
* Don's Donge checks security code and sends OK/NOK  : <-- ok       *
* IMME Sends Message Request for hello message (0)   : sendbyte --> *
* Don's Donge checks security code and sends OK/NOK  : <-- ok       *
* IMME Sends Done message                            : donebyte --> *
* Don's Donge checks security code and sends OK/NOK  : <-- ok       *
*                                                                   *
* Intent is to show several things to student.                      *
* - Radio interception                                              *
* - Packet based protocol                                           *
* - Fuzzable parameters in protocol exchange                        *
* - Extractable data from firmware (i.e. security code, hidden      *
*       messages)                                                   *
* - Security data left in source code                               *
*                                                                   *
* Future functionality:                                             *
* - User selects security code                                      *
* - User selects message request                                    *
*                                                                   *
****************************************************************** */

/* ******************************************************************
* Message Types - These define the type of message being built and  *
*                 transmitted.                                      *
****************************************************************** */

xdata u8 INITBYTE = 0; //0x00; //0x00
xdata u8 SECBYTE  = 1; //0x01; //0x01
xdata u8 SENDBYTE = 2; //0x02; //0x02
xdata u8 OKBYTE   = 105; //0x69; //0x69
xdata u8 NOKBYTE  = 153; //0x99; //0x99
xdata u8 HEADBYTE = 238; //0xEE; //0xEE
xdata u8 DONEBYTE = 255; //0xFF; //0xFF

/* ******************************************************************
* Data message references - These are the message requests. These   *
*                           are intended to simulate procedures or  *
*                           data requests.                          *
****************************************************************** */

#define PAD
#define PACKETPAD "SMASHSMASHSMASH"
xdata u8 HELLOMESS[4] =  "\x00\0"; //0; //0x00;     //FIXME: Null terminating these didn't work
xdata u8 ERRORMESS[4] =  "\x01\0"; //1; //0x01;
xdata u8 ATMESS[4]    =  "\x40\0"; //64; //0x40;
#ifdef LARGE_GAME
    xdata u8 BHMESS[4] = "\x42\0"; //66; //0x42;
    xdata u8 CUTMESS=[4] "\x43\0"; //67; //0x43;
    xdata u8 QMESS[4]  = "\x51\0"; //81; //0x51;
    xdata u8 TMESS[4]  = "\x54\0"; //84; //0x54;
#endif

/* ******************************************************************
* Messages Data - These are the messages that will be returned by   *
*                 the server radio. This mimics information         *
*                 request responses.                                *
****************************************************************** */
#define MESSMAX 16
xdata u8 errorword[6] = "ERROR";            //using 'magic' numbers to save space
#ifdef DDONSDONGLE // Not needed on IMME
    xdata u8 hello[6] = "Hello";
    xdata u8 atlas[13] ="@tlas Rules\n";
    #ifdef LARGE_GAME
        xdata u8 tybh[6] = "TYBH\n";
        xdata u8 cuts[14] = "Cutaway SMASH\n";
        xdata u8 qdog[15] = "Q's Dog: L1NK\n";
        xdata u8 travis[15] = "Good Neighbor\n";
    #endif
#endif

// Security Code
// Not needed on IMME, but since we need if for testing, what the hell...let's make a mistake
#ifdef LARGE_GAME
    //u32 SECCODE = 0xB347F337;
    xdata u8 SECCODE[6] = "\xb3\x47\xf3\x37\0";       //using 'magic' numbers to save space
#else
    //u16 SECCODE = 0xB347;
    xdata u8 SECCODE[4] = "\xb3\x47\0";               //using 'magic' numbers to save space
#endif

// CRC Seed
#ifdef LARGE_GAME
    //u16 CRCSEED = 0xFFFF; //0xFFFF
    //u16 CRCSEED = 65535; //0xFFFF
    xdata u8 CRCSEED[4] = "\xff\xff\0"; //0xFFFF
#endif

char* imme_title = "IMME Black Hat Game 2 v0.1";

/* Structure for storing data */
/* Struct doesn't seem to work. Therefore modify to plain old variables we manipulate */
typedef struct TransDataTag {
#ifdef LARGE_GAME
    u8 session; // This should actually be a random byte
#endif
    u8 plen;    // plen is the length of the packet data. Packet data is type and bufdata, therefor plen does NOT count itself
    u8 type;    // type is the type of message being sent. Init, security, send, done, ok/nok
    u8 bufdata[MESSMAX];   // If the message is going to contain data, this is where it will be held. Else it should be a 0 length string.
#ifdef LARGE_GAME
    u16 crc; // Chip handles, so we might not need this here
#endif
} TransData ;

/* ******************************************************************
* Radio Data - Initial Radio configuration settings                 *
****************************************************************** */
#define VAR_PACKET                  //Varible Packet
#define SYNC_BYTE0 0x7E             //Sync word is made up of two of these bytes
#define SYNC_BYTE1 0x7E             //Sync word is made up of two of these bytes
#define START_FREQ 915e6              // The initial freqency: e6 adds 6 zeros to the end making this 915000000
#ifdef SMODE_MSK
    #define SYNC_ON 0x73            // Turn SYNCWORD on and MSK
    #define SYNC_OFF 0x74           // Turn SYNCWORD off and MSK
#elif SMODE_GFSK
    #define SYNC_ON 0x13            // Turn SYNCWORD on and GFSK
    #define SYNC_OFF 0x14           // Turn SYNCWORD off and GFSK
#elif SMODE_ASKOOK
    #define SYNC_ON 0x33            // Turn SYNCWORD on and ASK/OOK
    #define SYNC_OFF 0x34           // Turn SYNCWORD off and ASK/OOK
#else
    #define SYNC_ON 0x03            // Turn SYNCWORD on and 2FSK
    #define SYNC_OFF 0x04           // Turn SYNCWORD off and 2FSK
#endif
